package com.orangelife.mobile.individual.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.curry.android.util.ImageDownloader;
import com.curry.android.util.ImageDownloader.ImageSize;
import com.curry.orangelife.mobile.R;
import com.curry.android.adapter.FatherAdapter;
import com.orangelife.mobile.constants.Constant;

import java.util.List;
import java.util.Map;

public class MyOrderDetailAdapter<T> extends FatherAdapter<T> {
	private Context context;
	private List<T> list;
	private Map<String, Object> map;
	private String orderType;
	private ImageDownloader image = ImageDownloader.getImageDownloader();

	public MyOrderDetailAdapter(Context context, List<T> list, String orderType) {
		this.context = context;
		this.list = list;
		this.orderType = orderType;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("ResourceAsColor")
	@SuppressWarnings("unchecked")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			/* 团购和商品 */
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_order_detail, null);
			viewHolder.ivGoods = (ImageView) convertView
					.findViewById(R.id.ivGoods);
			viewHolder.tvGroupSinglePrice = (TextView) convertView
					.findViewById(R.id.tvGoodsName);
			viewHolder.tvTotalPrice = (TextView) convertView
					.findViewById(R.id.tvMarkerPrice);
			viewHolder.tvDiscountPrice = (TextView) convertView
					.findViewById(R.id.tvUnitPrice);
//			viewHolder.ivOrder = (ImageView) convertView
//					.findViewById(R.id.iv_order_unselect);
			viewHolder.tvAllNumber = (TextView) convertView
					.findViewById(R.id.tvTotalNumber);
			viewHolder.tvSize = (TextView) convertView
					.findViewById(R.id.tvSpecsName);
			/** 线下活动 */
//			viewHolder.llCredit = (LinearLayout) convertView
//					.findViewById(R.id.ll_credit);
//			viewHolder.tvActiveName = (TextView) convertView
//					.findViewById(R.id.tv_active_name);
//			viewHolder.tvActiveTime = (TextView) convertView
//					.findViewById(R.id.tv_active_time_content);
//			viewHolder.viewDivider = convertView.findViewById(R.id.divider);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		map = (Map<String, Object>) list.get(position);
		if (orderType.equals(Constant.GOOD_TYPE)||orderType.equals(Constant.CREDIT_TYPE)) {
			/** 商品 */
//			viewHolder.tvAllNumber.setVisibility(View.VISIBLE);
//			viewHolder.llCredit.setVisibility(View.GONE);
			viewHolder.tvSize.setText(map.get("pdtDesc").toString());
			viewHolder.tvGroupSinglePrice.setText(map.get("goodsName")
					.toString());
			if(orderType.equals(Constant.GOOD_TYPE)){
			   viewHolder.tvDiscountPrice.setText(Constant.RMB_FLAG
					+ map.get("price").toString());
//			   viewHolder.tvTotalPrice.setText("￥"+ map.get("mkPrice").toString());
			}else{
				viewHolder.tvDiscountPrice.setText(
						map.get("price").toString()+"积分");
			}
			image.download(map.get("cover").toString().trim(),
					viewHolder.ivGoods,
					ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
		} else if (orderType.equals(Constant.LINE_TYPE)) {
			/*线下活动 */
//			viewHolder.llCredit.setVisibility(View.VISIBLE);
//			viewHolder.tvActiveName.setText(map.get("actvtName").toString());
//			viewHolder.tvActiveTime.setText(DateUtil.getInstance()
//					.tranLongForDate2(
//							Long.valueOf(map.get("endTime").toString())));
			// viewHolder.tvPhone.setText(map.get("hotline").toString());
		} else {
			/*团购 */
//			viewHolder.llCredit.setVisibility(View.GONE);
			viewHolder.tvSize.setText(map.get("pdtDesc").toString());
//			viewHolder.tvGroupSinglePrice.setText(map.get("groupName")
//					.toString());
			viewHolder.tvGroupSinglePrice.setText(map.get("goodsName")
					.toString());
			viewHolder.tvDiscountPrice.setText(Constant.RMB_FLAG
					+ map.get("price").toString());
			viewHolder.tvTotalPrice.setText(Constant.RMB_FLAG+ map.get("mkPrice").toString());
			viewHolder.tvAllNumber.setText("X"
					+ map.get("goodsCount").toString());
//			image.download(map.get("groupLogo").toString().trim(),
//					viewHolder.ivGoods,
//					ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
			image.download(map.get("cover").toString().trim(),
					viewHolder.ivGoods,
					ImageSize.IMAGE_SIZE_EXTRA_SMALL_RECTANGLE_JPG);
		}
		viewHolder.tvTotalPrice.getPaint().setFlags(
				Paint.STRIKE_THRU_TEXT_FLAG);
		viewHolder.tvAllNumber.setText("数量"
				+"X"+map.get("goodsCount").toString());
//		viewHolder.ivOrder.setVisibility(View.GONE);
		/* 处理自定义分割线，最后一行的分割线隐藏 */
//		if ((position + 1) == list.size()) {
//			viewHolder.viewDivider.setVisibility(View.GONE);
//		} else {
//			viewHolder.viewDivider.setVisibility(View.VISIBLE);
//		}
		return convertView;
	}

	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = list;
		return MyOrderDetailAdapter.this;
	}

	class ViewHolder {
		/** 团购和商品 */
//		private LinearLayout llCredit;
//		private ImageView ivOrder;
		private ImageView ivGoods;
		private TextView tvGroupSinglePrice;
		private TextView tvTotalPrice;
		private TextView tvDiscountPrice;
		private TextView tvAllNumber;
		private TextView tvSize;

		/** 线下活动 */
//		private TextView tvActiveName;
//		private TextView tvActiveTime;
//		private View viewDivider;
	}

}
